from typing import Any, Optional

from pydantic import BaseModel


class APIResponse(BaseModel):
    code: int
    msg: str
    data: Optional[Any] = None

    @classmethod
    def ok(cls, data: Any = None, msg: str = "Success") -> "APIResponse":
        """成功响应: code=200, msg=自定义（默认"Success"), data=传入值"""
        return cls(code=200, msg=msg, data=data)

    @classmethod
    def err(cls, code: int = 400, msg: str = "Error") -> "APIResponse":
        """错误响应: code=自定义(默认400), msg=自定义（默认"Error"), data=None"""
        return cls(code=code, msg=msg, data=None)
